package com.zb.kc.ucenter.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zb.kc.commonutils.JwtUtils;
import com.zb.kc.commonutils.MD5;
import com.zb.kc.commonutils.R;
import com.zb.kc.commonutils.ResultCode;
import com.zb.kc.servicebase.exceptionhandler.KgcException;
import com.zb.kc.ucenter.entity.UcenterMember;
import com.zb.kc.ucenter.dao.UcenterMemberDao;
import com.zb.kc.ucenter.entity.vo.LoginVo;
import com.zb.kc.ucenter.entity.vo.RegisterVo;
import com.zb.kc.ucenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author wanggen
 * @since 2020-04-20
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberDao, UcenterMember> implements UcenterMemberService {

    @Override
    public String login(LoginVo member) {
        String mobile = member.getMobile();
        String password = member.getPassword();
        // 判断手机号和密码非空判断
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)) {
            throw new KgcException(ResultCode.PHONR_OR_PASSWORD_NULL);
        }
        // 判断手机号是否正确
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", mobile);
        UcenterMember ucenterMember = baseMapper.selectOne(wrapper);
        if (ObjectUtil.isNull(ucenterMember)) {
            throw new KgcException(ResultCode.MOBILE_NOTFOUND);
        }
        // 判断用户是否禁用
        if (ucenterMember.getDisabled()) {
            throw new KgcException(ResultCode.USER_DISABLED);
        }
        // 判断密码
        if (!MD5.encrypt(password).equals(ucenterMember.getPassword())) {
            throw new KgcException(ResultCode.PASSWORD_ERROR);
        }
        // 登录成功，生成token字符串
        String jwtToken = JwtUtils.getJwtToken(ucenterMember.getId(), ucenterMember.getNickname());
        return jwtToken;
    }

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public void register(RegisterVo registerVo) {
        //获取注册信息，进行校验
        String nickname = registerVo.getNickname();
        String mobile = registerVo.getMobile();
        String password = registerVo.getPassword();
        String code = registerVo.getCode();

        //校验参数
        if (StringUtils.isEmpty(mobile) ||
                StringUtils.isEmpty(mobile) ||
                StringUtils.isEmpty(password) ||
                StringUtils.isEmpty(code)) {
            throw new KgcException(20001, "error");
        }

        //校验校验验证码
        //从redis获取发送的验证码
        String mobleCode = redisTemplate.opsForValue().get(mobile);
        if (!code.equals(mobleCode)) {
            throw new KgcException(20001, "error");
        }

        //查询数据库中是否存在相同的手机号码
        Integer count = baseMapper.selectCount(new QueryWrapper<UcenterMember>().eq("mobile", mobile));
        if (count.intValue() > 0) {
            throw new KgcException(20001, "error");
        }

        //添加注册信息到数据库
        UcenterMember member = new UcenterMember();
        member.setNickname(nickname);
        member.setMobile(registerVo.getMobile());
        member.setPassword(MD5.encrypt(password));
        member.setDisabled(false);
        member.setAvatar("https://kgc-wg2020.oss-cn-shanghai.aliyuncs.com/2020/04/08/6839f23717b1445b87d73bf15528a0c101.jpg");
        this.save(member);
    }

    @Override
    public LoginVo getLoginInfo(String memberId) {
        UcenterMember member = baseMapper.selectById(memberId);
        LoginVo loginInfoVo = new LoginVo();
        BeanUtils.copyProperties(member, loginInfoVo);
        return loginInfoVo;
    }

    @Override
    public UcenterMember getByOpenid(String openid) {
        QueryWrapper<UcenterMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("openid", openid);
        UcenterMember member = baseMapper.selectOne(queryWrapper);
        return member;
    }

    @Override
    public Map<String, Object> getUserLessInfo(String id) {
        Map<String, Object> map = baseMapper.getUserLessInfo(id);
        return map;
    }

    @Override
    public Integer countRegisterDay(String day) {
        return baseMapper.countRegisterDay(day);
    }

}
